﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.DAL;
using rock.common;
using rock.BLL;

namespace rock.UI.houseinfo
{
    public partial class UserControlHouseXiaoqu : UserControl
    {
        DalHOUSE_XIAOQU dal = new DalHOUSE_XIAOQU();
        public UserControlHouseXiaoqu()
        {
            InitializeComponent();
            this.userControlCommonListHeader1.addClick = addClick;
            this.userControlCommonListHeader1.updateClick = updateClick;
            this.userControlCommonListHeader1.deleteClick = deleteClick;
            this.userControlCommonListHeader1.searchClick = searchClick;
            this.Dock = System.Windows.Forms.DockStyle.Fill;
            
            ViewUtils.setDataGridViewStlye(this.dataGridView1);
        }
        //刷新数据
        public void refreshData()
        {

            String search =  this.userControlCommonListHeader1.textBox1.Text;
            DataSet ds = dal.GetList("NAME LIKE '%" + search + "%'  ORDER BY HOUSE_XIAOQU.NAME ASC ");
            this.dataGridView1.DataSource = ds.Tables[0];
        }


        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            updateClick();
        }
        private void addClick()
        {
            FormHouseXiaoquUpdate fcv = new FormHouseXiaoquUpdate();
            fcv.oneRefreshParentData = refreshData;
            fcv.ShowDialog(this);
        }
        private void updateClick()
        {
            if (dataGridView1.CurrentRow!=null)
            {
                int row = this.dataGridView1.CurrentRow.Index;
                int col = this.dataGridView1.CurrentCell.ColumnIndex;
                // String value = this.dataGridView1.SelectedRows[row].Cells["CODE_VALUE"].Value.ToString();
                
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();

                // this.textBox1.Text = value;
                FormHouseXiaoquUpdate fcv = new FormHouseXiaoquUpdate();



                fcv._ModelHOUSE_XIAOQU = dal.GetModel(Convert.ToInt32(seqid));
                //注册代理
                fcv.oneRefreshParentData = refreshData;
                fcv.ShowDialog(this);

            }
            else
            {
                MessageBox.Show("请选择要更新的记录.", "提示");
            }
        }
        private void deleteClick()
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                int seqidInt = Convert.ToInt32(seqid);
                if (seqidInt > 0)
                {

                    if (BllCommonUtil.Exists("SELECT COUNT(1) FROM HOUSE_DONG WHERE HOUSE_XIAOQU_SEQID = " + seqidInt))
                    {
                        MessageBox.Show("此市场信息存存排层,不能删除,如需删除,请先删除此市场下的排层记录", "提示");
                        return;

                    }

                    DialogResult dialogResult = MessageBox.Show("请确认是否要删除！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (dialogResult.Equals(DialogResult.OK))
                    {

                        

                        dal.Delete(seqidInt);
                        //刷新数据
                        refreshData();
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的记录.", "提示");
            }

        }

        private void searchClick()
        {
             
            refreshData();
        }

        private void UserControlHouseXiaoqu_Load(object sender, EventArgs e)
        {
            refreshData();
        }
        /// <summary>
        /// 双击更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
        {
            this.updateClick();
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

         
        
    }
}
